package net.interneteidee.utility;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.validation.constraints.AssertTrue;

import org.richfaces.component.UIExtendedDataTable;

import net.interneteidee.entity.Categoria;
import net.interneteidee.entity.Cliente;
import net.interneteidee.entity.Famiglia;
import net.interneteidee.entity.Fattura;
import net.interneteidee.entity.LineaOrdine;
import net.interneteidee.entity.Ordine;
import net.interneteidee.entity.Prodotto;
import net.interneteidee.servizi.ProdottoCategoriaFamigliaService;

@ManagedBean
@SessionScoped
public class TabRisultati<T> extends GenericTab implements ComponentName {

	private List<T> risultati;
	private int page = 1;
	private T object;
	
	protected String selectionMode = "single";
	protected Collection<Object> selection;
	private String tabella;
	
	public TabRisultati() {
		// TODO Auto-generated constructor stub
		risultati = new LinkedList<T>();
		
	}
	public List<T> getRisultati() {
		return risultati;
	}
	public void setRisultati(List<T> risultati) {
		this.risultati = risultati;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
		
	public void tableListener(AjaxBehaviorEvent event) {
		UIExtendedDataTable dataTable = (UIExtendedDataTable) event.getComponent();
		Object originalKey = dataTable.getRowKey();
		initObject(dataTable);
		for (Object selectionKey : selection) {
			dataTable.setRowKey(selectionKey);
			if (dataTable.isRowAvailable()) {
				getObject(dataTable);
			}
		}
		dataTable.setRowKey(originalKey);
	}
	
	public void initObject(UIExtendedDataTable dataTable){
		
		if(dataTable.getId().equals(tabellaClienti))
			object = (T) new Cliente();
		else if(dataTable.getId().equals(tabellaFamiglie))
			object = (T) new Famiglia();
		else if(dataTable.getId().equals(tabellaCategorie))
			object = (T) new Categoria();
		else if(dataTable.getId().equals(tabellaProdotti))
			object = (T) new Prodotto();

	}
	
	public void getObject(UIExtendedDataTable dataTable){
		
		if(dataTable.getId().equals(tabellaClienti))
			object =  (T) dataTable.getRowData();
		else if(dataTable.getId().equals(tabellaFamiglie))
			object =  (T) dataTable.getRowData();
		else if(dataTable.getId().equals(tabellaCategorie))
			object =  (T) dataTable.getRowData();
		else if(dataTable.getId().equals(tabellaProdotti))
			object = (T) dataTable.getRowData();

		
	}
	
	public void setNewObject(String idTab){
		if(idTab.contains("Cliente"))
			object = (T) new Cliente();
		else if(idTab.contains("Famiglia"))
			object = (T) new Famiglia();
		else if(idTab.contains("Categoria"))
			object = (T) new Categoria();
		else if(idTab.contains("Prodotto"))
			object = (T) new Prodotto();
		else if(idTab.contains("Ordine"))
			object = (T) new Ordine();
		else if(idTab.contains("Fattura"))
			object = (T) new Fattura();
	}
	
	
	

	/**
	 * @return the object
	 */
	public T getObject() {
		return object;
	}
	/**
	 * @param object the object to set
	 */
	public void setObject(T object) {
		this.object = object;
	}
	/**
	 * @return the selectionMode
	 */
	public String getSelectionMode() {
		return selectionMode;
	}
	/**
	 * @param selectionMode the selectionMode to set
	 */
	public void setSelectionMode(String selectionMode) {
		this.selectionMode = selectionMode;
	}
	/**
	 * @return the selection
	 */
	public Collection<Object> getSelection() {
		return selection;
	}
	/**
	 * @param selection the selection to set
	 */
	public void setSelection(Collection<Object> selection) {
		this.selection = selection;
	}
	/**
	 * @return the tabella
	 */
	public String getTabella() {
		return tabella;
	}
	/**
	 * @param tabella the tabella to set
	 */
	public void setTabella(String tabella) {
		this.tabella = tabella;
	}
	
	
}
